export default {
    data() {
        return {
            tableData: [],
            page: 1,
            limit: 10,
            count: 0,
            type:0,
            operatorList:[],                    //供应商列表
            operator_id:"",
            name:'',
            date:'',
            add_radio:false
        }
    },
    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        that.getList();
        that.getOperatorList();
    },

    //方法
    methods: {
        /**
         * 选择类型
         * @param tab
         */
        handleClick(tab) {
            let that = this;
            that.type = tab;
            that.getList();
        },

        /**
         * 筛选
         */
        search:function () {
            this.getList();
        },

        /**
         * 列表
         */
        getList() {
            var that = this;
            that.tableData = [];
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.type = 2;
            formData.menustatus = that.type;
            //时间条件
            if (that.date) {
                formData.start_time = that.formatDateTime(that.date[0]);
                formData.end_time = that.formatDateTime(that.date[1]);
            }
            if (that.name) {
                formData.keys = that.name;
            }
            if (that.operator_id) {
                formData.seller_id = that.operator_id;
            }
            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Publics/flashoverlist", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body) {
                        for (var i in res.body.list) {
                            if (res.body.list[i].status === 1) {
                                var status_text = '上架';
                            } else {
                                var status_text = '下架';
                            }
                            if (res.body.list[i].stageName === '') {
                                var stageName = '--';
                            } else {
                                var stageName = res.body.list[i].stageName;
                            }
                            if (res.body.list[i].is_audit === 1) {
                                var audit_text = "<span style='color: #67C23A;'>审核通过</span>";
                            } else if (res.body.list[i].is_audit === 2){
                                var audit_text = "<span style='color: #F56C6C;'>审核未通过</span>";
                            }else {
                                var audit_text = "<span style='color: #f5a623;'>待审核</span>";
                            }

                            that.tableData.push({
                                i: i,
                                id: res.body.list[i].goods_id,
                                img: res.body.list[i].picture[0],
                                cost_price: res.body.list[i].cost_price,
                                title: res.body.list[i].title,
                                sn: res.body.list[i].sn,
                                submit_time: res.body.list[i].submit_time,
                                sellerName: res.body.list[i].sellerName,
                                is_audit: res.body.list[i].is_audit,
                                is_status: res.body.list[i].status,
                                is_add: res.body.list[i].isAdd,
                                subject_id: res.body.list[i].subject_id,
                                stageName: stageName,
                                status: status_text,
                                audit_text: audit_text,
                            });
                        }
                        that.count = res.body.count;
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 添加广告
         */
        add: function () {
            this.$router.push('/sowing/add_swiper');
        },

        /**
         * 修改
         * @param id
         */
        edit: function (id) {
            var that = this;
            that.$router.push({path: '/sowing/add_swiper', query: {swiper_id: id}});
        },

        /**
         * 删除
         * @param e
         */
        deleteRow: function (e) {
            var that = this;
            that.$confirm('此操作将永久删除该图片, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                that.doDelete(e);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //执行删除
        doDelete: function (e) {
            var that = this;
            //请求登陆接口
            that.$http.post(that.adminApi.api_url + "/Bannerimg/del", {
                token: that.token,
                id: e
            }, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '删除成功' }`
                    });
                    that.getList();
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 下一页
         * @param currentPage
         */
        handleCurrentChange: function (currentPage) {
            var that = this;
            that.page = currentPage;
        },

        /**
         * 供应商列表
         */
        getOperatorList:function () {
            const that =this;
            const formData = {};
            formData.token = that.token;
            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Operator/availablelist", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    if (res.body) {
                        for (var i in res.body) {
                            that.operatorList.push({
                                id: res.body[i].id,
                                name: res.body[i].company,
                            });
                        }
                    }
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 时间转换
         * @param date
         * @returns {string}
         */
        formatDateTime: function(date) {
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            var d = date.getDate();
            d = d < 10 ? ('0' + d) : d;
            var h = date.getHours();
            var minute = date.getMinutes();
            minute = minute < 10 ? ('0' + minute) : minute;
            return y + '-' + m + '-' + d;
        },

        /**
         * 跳转预览
         */
        toPreview:function(e) {
            this.$router.push({path:'/flashover/preview',query:{goods_id:e}})
        },


        /**
         * 添加文章音频
         */
        add_interest:function (e) {
            this.$router.push({path:'/interest/add_interest',query:{goods_id:e}})
        },

        /**
         * 修改文章
         */
        edit_interest:function (subject_id,e) {
            this.$router.push({path:'/interest/add_interest',query:{goods_id:e,subject_id:subject_id}})
        },


        /**
         * 下线
         */
        under_line:function (e) {
            const that =this;
            that.$confirm('确定下线该产品?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                const formData = {};
                formData.token = that.token;
                formData.goods_id = e;
                that.$http.post(that.adminApi.api_url + "/Publics/downLine", formData, {
                    emulateJSON: true
                }).then(
                    function (res) {
                        // 处理成功的结果
                        that.$message({
                            type: 'success',
                            message: `操作提示: ${ '下线成功' }`
                        });
                        that.getList();
                    },
                    function () {
                        // 处理失败的结果
                        that.$message({
                            type: 'error',
                            message: `操作提示: ${ '处理异常' }`
                        });
                    });
            })
        }
    }
}

